<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\AgentSmsSevice;
use app\model\SmsModule as Model;
use think\Request;

/**
 * @apiDefine admin
 */
class SmsModule extends Controller
{
    /*
     * @api {GET} /admin/smsModule/ 短信通知列表
     * @apiName 短信通知列表 短信通知列表
     * @apiGroup admin
     * @apiParam {Number} [page=1] 页码
     * @apiParam {Number} [list_rows=10] 分页大小
     * @apiParam {String} [todo_time[start]] 发送时间起始
     * @apiParam {String} [todo_time[end]] 发送时间起始
     * @apiParam {Number=0,1} [handle_status] 发送状态 0未发送 1 已发送
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $param = $request->only(['todo_time' => [], 'handle_status']);

        $paginate = $request->only(['page' => 1, 'list_rows' => 10]);
        $where = [];
        $whereNull = [];
        if (isset($param['todo_time']['start']) && $param['todo_time']['start']) {
            $where[] = ['todo_time', '>=', $param['todo_time']['start']];
        }
        if (isset($param['todo_time']['end']) && $param['todo_time']['end']) {
            $where[] = ['todo_time', '<=', $param['todo_time']['end']];
        }
        if (isset($param['handle_status'])) {
            $whereNull['handle_time'] = $param['handle_status'];
        }

        return success('', (new Model())->adminModuleList($where, $paginate, $whereNull, 'id,content'));
    }

    /*
     * @api {GET} /admin/smsModule/save 新建或者保存
     * @apiName 供应商保存
     * @apiGroup admin
     * @apiParam {Number} [id] id 更新时候传递
     * @apiParam {String} content 内容
     * @apiParam {String} todo_time 发送时间
     * @apiParam {String} agent_ids 代理商id
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function save(Request $request)
    {
        $rules = [
            'content' => 'require',
            'todo_time' => 'require',
            'agent_ids' => 'require',
        ];
        $messages = [
            'content.require' => '请填写内容',
            'todo_time.require' => '请填写发送时间',
            'agent_ids.require' => '请选择代理商',
        ];
        $param = $request->only(array_keys($rules));
        if ($msg = dmjValidate($rules, $messages, $param)) {
            return fail($msg);
        }

        // 短信内容校验，暂不校验手机号个数，个数超过5000则分成两次发送
        if (mb_strlen($request->param('content')) > 268) {
            return fail('短信内容不能超过268个字(包括符号和空格)');
        }
    }

    /*
     * @api {GET} /admin/smsModule/read 短信通知详情
     * @apiName 短信通知详情
     * @apiGroup admin
     * @apiParam {Number} id id
     * @apiParam {Number=0,1,2} [log_status] 发送状态 0 未发送 1 发送成功 2发送失败
     * @apiParam {String} [mobile] 手机号
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function read(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('缺少必要参数');
        }

        $where = [];
        if ($request->has('log_status')) {
            $where[] = ['status', '=', $request->param('log_status')];
        }
        if ($request->has('mobile')) {
            $where[] = ['mobile', 'like', '%'.$request->param('mobile').'%'];
        }

        $data = Model::with(['log' => function ($query) use ($where) {
            if ($where) {
                $query = $query->where($where);
            }
            $query->whereNull('deleted_at')->order('send_time', 'desc');
        }])->find($id);

        if ($data) {
            return success('', $data->toArray());
        } else {
            return fail('数据不存在');
        }
    }

    /*
     * @api {POST} /admin/smsModule/delete 删除短信通知
     * @apiName 供应商删除
     * @apiGroup admin
     * @apiParam {Number} id id
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function delete(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('缺少必要参数');
        }

        if (Model::destroy($id)) {
            return success();
        }

        return fail();
    }

    /*
     * @api {POST} /admin/smsModule/sendMsg 立刻通知
     * @apiName 立刻通知
     * @apiGroup admin
     * @apiParam {Number} id id
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function sendMsg(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('缺少必要参数');
        }

        $res = (new AgentSmsSevice())->moduleSend(Model::where('id', $id)->select());
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }
}
